//app.js
var QQMapWX = require('./utils/qqmap-wx-jssdk.js');
var qqmapsdk;
App({
  onLaunch: function () {
    console.log('11111');
  },

  setCity(callback) {
    let _this=this;
    var city = wx.getStorageSync('city');
    if (city){
      callback(city);
    }else{
      wx.getSetting({
        success: res => {
          if (typeof (res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
            wx.showModal({
              title: '提示',
              content: '您拒绝了定位权限，将无法使用XX功能1',
              success: res => {
                if (res.confirm) {
                  _this.openSetting(function (res) {
                    callback(456);
                  });
                } else {
                  callback('拒绝了定位权限')
                  wx.showToast({
                    title: '您拒绝了定位权限，将无法使用XX功能',
                    icon: 'none'
                  });
                }
              }
            });
          } else {
            _this.getLocation(function (res) {
              callback(res);
            })

          }
        }
      })
    }

  },

  //获取位置
  getLocation(back){
    let _this=this;
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'BH5BZ-4OCWU-WKSV2-2XBJF-3GV76-PRB6Y'
    })
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: latitude,
              longitude: longitude
            },
            success: function (res) {
              wx.setStorageSync('city', res.result.ad_info.city)
              back(res.result.ad_info.city);
            },
            fail: function (res) {
              back(res);
            }
          });
        }, fail: e => {
          back('false2')
            // 没有允许定位权限
            wx.showToast({
              title: '您拒绝了定位权限，将无法使用XX功能',
              icon: 'none'
            });
        }
    })
  },

  //跳转位置设置页面
  openSetting(back){
    let _this = this;
    // 跳转设置页面
    wx.openSetting({
      type: 'wgs84',
      success: res => {
        if (res.authSetting['scope.userLocation']) {
          // 授权成功，重新定位
          _this.getLocation(function(res){
            back(res)
          });
        }else{
          back(false)
          wx.showToast({
              title: '您未开启定位权限，将无法使用XX功能',
              icon: 'none'
            });

        }
      }
    });
  },
  wxRequest(method, url, data, callback, errFun){
    var that = this;
    wx.request({
      url:that.globalData.theUrl1+url,
      method: method,
      header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data: data,
      timeout:30000,
      success: function (res) {
        callback(res);
      },
      fail: function (err) {
        errFun(err)
      }
    })
  },
  globalData: {
    userInfo: null,
    theUrl1: 'https://bmfw.99fuku.com/index.php',
    api: 'https://bmfw.99fuku.com/index.php/Samecity/',
  }
})